package net.kleinhenz.norabase.client;

import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.types.FormMethod;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.types.Alignment;

public class ImportBattlegroupDialog extends Window {

	public ImportBattlegroupDialog(final BattlegroupTabSet tabSet) {

		setWidth("500px");
		setIsModal(false);
		setShowBody(true);
		setShowFooter(false);
		setShowHeaderBackground(false);
		setShowHeaderIcon(false);
		setShowMinimizeButton(false);
		setShowStatusBar(true);
		setShowTitle(true);
		centerInPage();
		setTitle("Import Battlegroup");
        setAutoSize(true);  
        setCanDragReposition(true);  
        setCanDragResize(false);  
        setMembersMargin(10);
        
        VLayout layout = new VLayout(10);
        layout.setWidth("100%");
        layout.setMargin(10);
        
        final DynamicForm dynamicForm = new DynamicForm();
        dynamicForm.setEncoding(Encoding.MULTIPART);
        dynamicForm.setTarget("_blank");
        dynamicForm.setMethod(FormMethod.POST);
        dynamicForm.setAction("/norabase/import");
        HiddenItem typeItem = new HiddenItem("type");
    	typeItem.setValue("bg");

        final TextItem nameField = new TextItem("name", "Battlegroup&nbsp;Name");
        nameField.setRequired(true);
        nameField.setValidators(new NullValidator());
        nameField.setWidth("100%");
        
        final UploadItem fileField = new UploadItem("file", "CSV&nbsp;File");
        fileField.setRequired(true);
        fileField.setValidators(new NullValidator());
        fileField.setWidth("100%");
        
        dynamicForm.setFields(new FormItem[] { nameField, fileField, typeItem});
        
        layout.addMember(dynamicForm);
        
        HLayout btnLayout = new HLayout(10);
        btnLayout.setAlign(Alignment.RIGHT);
        btnLayout.setDefaultLayoutAlign(VerticalAlignment.CENTER);
        btnLayout.setMembersMargin(10);
        
        Button btnSave = new Button("Import");
        btnSave.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				dynamicForm.submitForm();
				ImportBattlegroupDialog.this.destroy();
			}
		});
        btnLayout.addMember(btnSave);
        
        Button btnCancel = new Button("Cancel");
        btnCancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ImportBattlegroupDialog.this.destroy();
			}
		});
        btnLayout.addMember(btnCancel);

        layout.addMember(btnLayout);
        addItem(layout);
	}
}
